package activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import net.ck.hzq.R;

import org.greenrobot.eventbus.EventBus;

import Utils.HelloWordModel;
import Utils.StringUtil;
import Utils.ToastUtil;
import base.BaseActivity;
import dao.BaseOutput;
import dao.WorkOrderDetail;
import dao.WorkOrderDetailInput;
import dao.WorkOrderDetailOutput;
import event.LoginEvent;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class WorkOrderDetailActivity extends BaseActivity implements View.OnClickListener {

    private int mWorkOrderId;

    TextView workOrderType;
    TextView workOrderContent;
    TextView merchantName;
    TextView merchantNo;
    TextView tradeCardNo;
    TextView tradeAmount;
    TextView tradeTime;
    ViewGroup btnLayout;
    TextView cancelBtn;
    TextView confirmBtn;

    public static void start(Context context, int workOrderId) {
        Intent intent = new Intent(context, WorkOrderDetailActivity.class);
        intent.putExtra("workOrderId", workOrderId);
        context.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_work_order_detail;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mWorkOrderId = getIntent().getIntExtra("workOrderId", 0);

        workOrderType = findViewById(R.id.work_order_type);
        workOrderContent = findViewById(R.id.work_order_content);
        merchantName = findViewById(R.id.merchant_name);
        merchantNo = findViewById(R.id.merchant_no);
        tradeCardNo = findViewById(R.id.trade_card_no);
        tradeAmount = findViewById(R.id.trade_amount);
        tradeTime = findViewById(R.id.trade_time);

        btnLayout = findViewById(R.id.btn_layout);
        cancelBtn = findViewById(R.id.btn_yes);
        confirmBtn = findViewById(R.id.btn_no);

        cancelBtn.setOnClickListener(this);
        confirmBtn.setOnClickListener(this);
        findViewById(R.id.iv_back).setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        queryDetail(mWorkOrderId);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_no:
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_yes:
                WorkOrderHandleActivity.start(this, mWorkOrderId);
                break;
        }
    }

    private void queryDetail(int workOrderId) {
        showProgressDialog();
        WorkOrderDetailInput input = new WorkOrderDetailInput();
        input.workOrderId = workOrderId;
        HelloWordModel.getInstance(this).getWorkOrderDetail(input).enqueue(new Callback<BaseOutput<WorkOrderDetailOutput>>() {
            @Override
            public void onResponse(Call<BaseOutput<WorkOrderDetailOutput>> call,
                                   Response<BaseOutput<WorkOrderDetailOutput>> response) {
                dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                try {
                    if (response.body() != null && "401".equals(response.body().result)) {
                        EventBus.getDefault().post(new LoginEvent());
                    } else if (response.body().msg.equals("success") && response.body().data != null) {
                        if (response.body().data.items != null && response.body().data.items != null) {
                            showDetailInfo(response.body().data.items);
                        }
                    } else {
                        ToastUtil.showNormal(WorkOrderDetailActivity.this, response.body().msg);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<BaseOutput<WorkOrderDetailOutput>> call, Throwable t) {
                dismissProgressDialog();
            }
        });
    }

    private void showDetailInfo(WorkOrderDetail detail) {
        workOrderType.setText("工单类型：" + detail.workOrderType);
        workOrderContent.setText("工单内容：" + detail.content);
        merchantName.setText("商户姓名：" + detail.merchantName);
        merchantNo.setText("商户编号：" + detail.merchantNo);
        tradeCardNo.setText("交易卡号：" + detail.cardNo);
        tradeAmount.setText("交易金额：¥" + StringUtil.getFormatPrice(detail.tMoney));
        tradeTime.setText("交易时间：" + detail.transactionTime);

        btnLayout.setVisibility(detail.workOrderState == 1 ? View.VISIBLE : View.GONE);
    }

}
